package com.yst.shader.feature.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import com.yst.shader.R

/**
 * @author: xuyujian@ysten.com
 *  @date: 2020/8/18 22:06
 */
class CanvasView : AppCompatImageView {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    private val source by lazy { BitmapFactory.decodeResource(resources, R.drawable.test) }
    private val focus by lazy { BitmapFactory.decodeResource(resources, R.drawable.focus_red) }
    private val focusDrawable by lazy { resources.getDrawable(R.drawable.focus_red) }
    private val paint by lazy { Paint() }
    private val temp = Rect()
    private val outerRect = 100f

    init {

    }

    override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect)
        println("onFocusChanged $gainFocus , $direction , $previouslyFocusedRect")

//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
//            clipBounds = if (this.isFocused) {
//                Rect(-100, -100, width + 100, height + 100)
//            } else {
//                Rect(0, 0, width, height)
//                //  clipBounds = Rect(-50, -50, width + 50, height + 50)
//            }
//
//        }
    }


    override fun onDraw(canvas: Canvas) {

        if (this.isFocused) {

            focusDrawable.bounds = Rect(-150, -150, width + 50, height + 50)
            focusDrawable.draw(canvas)
//            paint.color = Color.GREEN
//            paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT)
//            canvas.drawCircle(width / 2f, height / 2f, width / 2f, paint)
//            paint.xfermode = null
        } else {
            //  clipBounds = Rect(-50, -50, width + 50, height + 50)
        }
        println("onDraw ${canvas.clipBounds} ,view clip $ ${this.clipBounds}")
        //canvas.drawBitmap(source, null, Rect(0, 0, width, height), paint)
        super.onDraw(canvas)
    }
}